﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using DocumentGain.model;
using DocumentGain;
using DocumentGain.dal;
using System.Configuration;
using System.Threading;
using Ican.YunPan.Services;
using Ican.YunPan.Services.GridFS;

namespace DocumentGain
{
    public partial class Leading_用户部门信息补充 : Form
    {
        string connstr = ConfigurationManager.AppSettings["MongoConnectionString"];
        string dbName = ConfigurationManager.AppSettings["MongoDBName"];
        string FilePath = ConfigurationManager.AppSettings["FilePath"];

        string cutTime = ConfigurationManager.AppSettings["cutTime"];
        string iscutTimebefore = ConfigurationManager.AppSettings["iscutTimebefore"];


        private delegate void SetControlTextCallback(Control o, string text);
        Service service = new Service();
        private string _allmessage = "";
        string LogDate = "";

        string nr = "";
        public Leading_用户部门信息补充()
        {
            InitializeComponent();
        }

         public void DataLeadingin(object o)
        {
            MessageShow("开始导入用户或部门补充数据");
            string sql = @"select distinct id,orgname,orgid,ParticipantsId,ParticipantsInfo,TableName from View_WorkFlow_Sc 
where id not in (
select distinct id from View_WorkFlow_Sc where orgname != '' and orgid != '' and ParticipantsId != '' and ParticipantsInfo != '')";
            DataTable dt = sqlHelpIcanYunPan.Query(sql).Tables[0];
            WriteLogFile("有" + dt.Rows.Count+"条数据待导入");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                MessageShow("开始导入" + i.ToString() + "条数据");
                string id = dt.Rows[i]["id"].ToString();
                string orgname = dt.Rows[i]["orgname"].ToString();
                string orgid = dt.Rows[i]["orgid"].ToString();
                string ParticipantsId = dt.Rows[i]["ParticipantsId"].ToString();
                string ParticipantsInfo = dt.Rows[i]["ParticipantsInfo"].ToString().Replace("微软需求_", "");
                string TableName = dt.Rows[i]["TableName"].ToString();

                if (ParticipantsId != "")
                {
                    string sql1 = @"select YP_Org.ID as OrgId,YP_Org.Name as OrgName,YP_User.Id as UserId,YP_User.Name as UserName from YP_User,YP_Org where YP_User.SourceParentID = YP_Org.ID and YP_User.Id = '" + ParticipantsId + "'";
                    DataTable dt1 = sqlHelpIcanYunPan.Query(sql1).Tables[0];
                    if (dt1.Rows.Count > 0)
                    {
                        string sqlupdate = "update " + TableName + " set orgname = '" + dt1.Rows[0]["OrgName"].ToString() + "',orgid='" + dt1.Rows[0]["OrgId"].ToString() + "',ParticipantsInfo = '" + dt1.Rows[0]["UserName"].ToString() + "' where id = '" + id + "'";
                        sqlHelpIcanYunPan.ExecuteSql(sqlupdate);
                    }
                    else if (orgname != "")
                    {
                        if (orgname == "武汉地铁")
                        {
                            string sqlupdate = "update " + TableName + " set orgid='927FEA30-9944-4D40-8EFC-8CB89CE7A622' where id = '" + id + "'";
                            sqlHelpIcanYunPan.ExecuteSql(sqlupdate);

                            string sql4 = @"select YP_Org.ID as OrgId,YP_Org.Name as OrgName,YP_User.Id as UserId,YP_User.Name as UserName from YP_User,YP_Org where YP_User.ParentID = YP_Org.ID and YP_User.Id = '" + ParticipantsId + "'";
                            DataTable dt4 = sqlHelpIcanYunPan.Query(sql4).Tables[0];
                            if (dt4.Rows.Count == 1)
                            {
                                string sqlupdate1 = "update " + TableName + " set orgname = '" + dt4.Rows[0]["OrgName"].ToString() + "',orgid='" + dt4.Rows[0]["OrgId"].ToString() + "',ParticipantsInfo = '" + dt4.Rows[0]["UserName"].ToString() + "' where id = '" + id + "'";
                                sqlHelpIcanYunPan.ExecuteSql(sqlupdate1);
                            }

                            string sql5 = "select ID,Name from YP_User where Id = '" + ParticipantsId + "'";
                            DataTable dt5 = sqlHelpIcanYunPan.Query(sql5).Tables[0];
                            if (dt5.Rows.Count == 1)
                            {
                                string sqlupdate2 = "update " + TableName + " set ParticipantsInfo = '" + dt5.Rows[0]["Name"].ToString() + "' where id = '" + id + "'";
                                sqlHelpIcanYunPan.ExecuteSql(sqlupdate2);
                            }
                        }
                        else
                        {
                            string sql3 = "select Id,Name from [dbo].[YP_Org] where Name = '" + orgname + "'";
                            DataTable dt3 = sqlHelpIcanYunPan.Query(sql3).Tables[0];
                            if (dt3.Rows.Count == 1)
                            {
                                string sqlupdate = "update " + TableName + " set orgid='" + dt3.Rows[0]["Id"].ToString() + "' where id = '" + id + "'";
                                sqlHelpIcanYunPan.ExecuteSql(sqlupdate);
                            }

                            string sql4 = @"select YP_Org.ID as OrgId,YP_Org.Name as OrgName,YP_User.Id as UserId,YP_User.Name as UserName from YP_User,YP_Org where YP_User.ParentID = YP_Org.ID and YP_User.Id = '" + ParticipantsId + "'";
                            DataTable dt4 = sqlHelpIcanYunPan.Query(sql4).Tables[0];
                            if (dt4.Rows.Count == 1)
                            {
                                string sqlupdate1 = "update " + TableName + " set orgname = '" + dt4.Rows[0]["OrgName"].ToString() + "',orgid='" + dt4.Rows[0]["OrgId"].ToString() + "',ParticipantsInfo = '" + dt4.Rows[0]["UserName"].ToString() + "' where id = '" + id + "'";
                                sqlHelpIcanYunPan.ExecuteSql(sqlupdate1);
                            }

                            string sql5 = "select ID,Name from YP_User where Id = '" + ParticipantsId + "'";
                            DataTable dt5 = sqlHelpIcanYunPan.Query(sql5).Tables[0];
                            if (dt5.Rows.Count == 1)
                            {
                                string sqlupdate2 = "update " + TableName + " set ParticipantsInfo = '" + dt5.Rows[0]["Name"].ToString() + "' where id = '" + id + "'";
                                sqlHelpIcanYunPan.ExecuteSql(sqlupdate2);
                            }

                            if (orgid != "")
                            {
                                string sql8 = @"select YP_Org.ID as OrgId,YP_Org.Name as OrgName,YP_User.Id as UserId,YP_User.Name as UserName 
from YP_User,YP_Org where YP_User.SourceParentID = YP_Org.ID and YP_User.Name = '" + ParticipantsInfo + @"' 
and YP_Org.ID = '" + orgid + "'";
                                DataTable dt8 = sqlHelpIcanYunPan.Query(sql8).Tables[0];
                                if (dt8.Rows.Count == 1)
                                {
                                    string sqlupdate = "update " + TableName + " set orgname = '" + dt8.Rows[0]["OrgName"].ToString() + "',orgid='" + dt8.Rows[0]["OrgId"].ToString() + "',ParticipantsInfo = '" + dt8.Rows[0]["UserName"].ToString() + "',ParticipantsId = '" + dt8.Rows[0]["UserId"].ToString() + "' where id = '" + id + "'";
                                    sqlHelpIcanYunPan.ExecuteSql(sqlupdate);
                                }
                            }
                        }
                    }
                }
                else if (ParticipantsInfo != "")
                {
                    string sql2 = @"select YP_Org.ID as OrgId,YP_Org.Name as OrgName,YP_User.Id as UserId,YP_User.Name as UserName 
from YP_User,YP_Org where YP_User.SourceParentID = YP_Org.ID 
and YP_User.Alias in (
select LoginName from [dbo].[UserRelation] where UserName = '" + ParticipantsInfo + "')";
                    DataTable dt2 = sqlHelpIcanYunPan.Query(sql2).Tables[0];
                    if (dt2.Rows.Count == 1)
                    {
                        string sqlupdate = "update " + TableName + " set orgname = '" + dt2.Rows[0]["OrgName"].ToString() + "',orgid='" + dt2.Rows[0]["OrgId"].ToString() + "',ParticipantsInfo = '" + dt2.Rows[0]["UserName"].ToString() + "',ParticipantsId = '" + dt2.Rows[0]["UserId"].ToString() + "' where id = '" + id + "'";
                        sqlHelpIcanYunPan.ExecuteSql(sqlupdate);
                    }
                    else if (orgname != "")
                    {
                        if (orgname == "武汉地铁")
                        {
                            string sqlupdate = "update " + TableName + " set orgid='927FEA30-9944-4D40-8EFC-8CB89CE7A622' where id = '" + id + "'";
                            sqlHelpIcanYunPan.ExecuteSql(sqlupdate);
                        }
                        else
                        {
                            string sql3 = "select Id,Name from [dbo].[YP_Org] where Name = '" + orgname + "'";
                            DataTable dt3 = sqlHelpIcanYunPan.Query(sql3).Tables[0];
                            if (dt3.Rows.Count == 1)
                            {
                                string sqlupdate = "update " + TableName + " set orgid='" + dt3.Rows[0]["Id"].ToString() + "' where id = '" + id + "'";
                                sqlHelpIcanYunPan.ExecuteSql(sqlupdate);
                            }

                            string sql6 = "select ID,Name from YP_User where Name = '" + ParticipantsInfo + "'";
                            DataTable dt6 = sqlHelpIcanYunPan.Query(sql6).Tables[0];
                            if (dt6.Rows.Count == 1)
                            {
                                string sqlupdate = "update " + TableName + " set ParticipantsId = '" + dt6.Rows[0]["ID"].ToString() + "' where id = '" + id + "'";
                                sqlHelpIcanYunPan.ExecuteSql(sqlupdate);
                            }

                            if (orgid != "")
                            {
                                string sql8 = @"select YP_Org.ID as OrgId,YP_Org.Name as OrgName,YP_User.Id as UserId,YP_User.Name as UserName 
from YP_User,YP_Org where YP_User.SourceParentID = YP_Org.ID and YP_User.Name = '" + ParticipantsInfo + @"' 
and YP_Org.ID = '" + orgid + "'";

                                DataTable dt8 = sqlHelpIcanYunPan.Query(sql8).Tables[0];

                                if (dt8.Rows.Count == 1)
                                {
                                    string sqlupdate = "update " + TableName + " set orgname = '" + dt8.Rows[0]["OrgName"].ToString() + "',orgid='" + dt8.Rows[0]["OrgId"].ToString() + "',ParticipantsInfo = '" + dt8.Rows[0]["UserName"].ToString() + "',ParticipantsId = '" + dt8.Rows[0]["UserId"].ToString() + "' where id = '" + id + "'";
                                    sqlHelpIcanYunPan.ExecuteSql(sqlupdate);
                                }
                            }
                        }
                    }
                    else
                    {
                        string sql7 = @"select YP_Org.ID as OrgId,YP_Org.Name as OrgName,YP_User.Id as UserId,YP_User.Name as UserName 
from YP_User,YP_Org where YP_User.SourceParentID = YP_Org.ID and YP_User.Name = '" + ParticipantsInfo + "'";
                        DataTable dt7 = sqlHelpIcanYunPan.Query(sql7).Tables[0];

                        if (dt7.Rows.Count == 1)
                        {
                            string sqlupdate = "update " + TableName + " set orgname = '" + dt7.Rows[0]["OrgName"].ToString() + "',orgid='" + dt7.Rows[0]["OrgId"].ToString() + "',ParticipantsInfo = '" + dt7.Rows[0]["UserName"].ToString() + "',ParticipantsId = '" + dt7.Rows[0]["UserId"].ToString() + "' where id = '" + id + "'";
                            sqlHelpIcanYunPan.ExecuteSql(sqlupdate);
                        }


                        string sql8 = @"select YP_Org.ID as OrgId,YP_Org.Name as OrgName,YP_User.Id as UserId,YP_User.Name as UserName 
from YP_User,YP_Org where YP_User.ParentID = YP_Org.ID and YP_User.Name = '" + ParticipantsInfo + "'";
                        DataTable dt8 = sqlHelpIcanYunPan.Query(sql8).Tables[0];

                        if (dt8.Rows.Count == 1)
                        {
                            string sqlupdate = "update " + TableName + " set orgname = '" + dt8.Rows[0]["OrgName"].ToString() + "',orgid='" + dt8.Rows[0]["OrgId"].ToString() + "',ParticipantsInfo = '" + dt8.Rows[0]["UserName"].ToString() + "',ParticipantsId = '" + dt8.Rows[0]["UserId"].ToString() + "' where id = '" + id + "'";
                            sqlHelpIcanYunPan.ExecuteSql(sqlupdate);
                        }

                        string sql9 = @"select YP_Org.ID as OrgId,YP_Org.Name as OrgName,YP_User.Id as UserId,YP_User.Name as UserName 
from YP_User,YP_Org where YP_User.ParentID = YP_Org.ID 
and YP_User.Alias in (
select LoginName from [dbo].[UserRelation] where UserName = '" + ParticipantsInfo + "')";
                        DataTable dt9 = sqlHelpIcanYunPan.Query(sql9).Tables[0];
                        if (dt9.Rows.Count == 1)
                        {
                            string sqlupdate = "update " + TableName + " set orgname = '" + dt9.Rows[0]["OrgName"].ToString() + "',orgid='" + dt9.Rows[0]["OrgId"].ToString() + "',ParticipantsInfo = '" + dt9.Rows[0]["UserName"].ToString() + "',ParticipantsId = '" + dt9.Rows[0]["UserId"].ToString() + "' where id = '" + id + "'";
                            sqlHelpIcanYunPan.ExecuteSql(sqlupdate);
                        }

                    }

                }

                //else if (orgname != "")
                //{
                //    if (orgname == "武汉地铁")
                //    {
                //        string sqlupdate = "update " + TableName + " set orgid='927FEA30-9944-4D40-8EFC-8CB89CE7A622' where id = '" + id + "'";
                //        sqlHelpIcanYunPan.ExecuteSql(sqlupdate);
                //    }
                //    else
                //    {
                //        string sql3 = "select Id,Name from [dbo].[YP_Org] where Name = '" + orgname + "'";
                //        DataTable dt3 = sqlHelpIcanYunPan.Query(sql3).Tables[0];
                //        if (dt3.Rows.Count == 1)
                //        {
                //            string sqlupdate = "update " + TableName + " set orgid='" + dt3.Rows[0]["Id"].ToString() + "' where id = '" + id + "'";
                //            sqlHelpIcanYunPan.ExecuteSql(sqlupdate);
                //        }
                //    }
                //}
            }
            MessageShow("导入完成");
        }


        private void button1_Click(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(DataLeadingin);
        }



        public void MessageShow(String input)
        {
            SetControlText(this.textBox1, input);
        }

        //写入文本日志
        public void WriteLogFile(String input)
        {
            SetControlText(this.textBox1, input);

            //指定日志文件的目录
            string fname = ConfigurationManager.AppSettings["LogPath"];

            //定义文件信息对象 
            FileInfo finfo = new FileInfo(fname + "Leading_PublicDoc" + DateTime.Now.ToString("yyyyMMdd") + ".txt");

            //判断文件是否存在以及是否大于2K 
            if (finfo.Exists && finfo.Length > 20480000)
            {
                //删除该文件 
                finfo.Delete();
            }
            //创建只写文件流 
            using (FileStream fs = finfo.OpenWrite())
            {
                //根据上面创建的文件流创建写数据流 
                StreamWriter w = new StreamWriter(fs);
                //设置写数据流的起始位置为文件流的末尾 
                w.BaseStream.Seek(0, SeekOrigin.End);

                w.Write("\r\n ");

                //写入日志内容并换行 
                w.Write(input + "\n ");
                //w.Write("--\n ");
                //清空缓冲区内容，并把缓冲区内容写入基础流 
                w.Flush();
                //关闭写数据流 
                w.Close();
            }
        }
        private void SetControlText(Control o, string text)
        {
            try
            {
                if (o.InvokeRequired)
                {
                    SetControlTextCallback d = new SetControlTextCallback(SetControlText);
                    this.Invoke(d, o, text);
                }
                else
                {
                    _allmessage = text + "\r\n" + _allmessage;
                    o.Text = _allmessage;
                }
            }
            catch { }
        }
    }
}
